package dao.impl;

import bean.Cinema;
import dao.CinemaDao;
import util.DBCon;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CinemaDaoImpl implements CinemaDao {
    @Override
    public List<Cinema> getAllCinema() {
        Connection con = DBCon.getConnection();
        if(con == null){
            System.out.println("数据库连接失败");
        }
        try {
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery("select * from table_cinema ");
            List<Cinema> cinemaList = new ArrayList<>();
            while (rs.next()){
                 Cinema cinema = new Cinema();
                 cinema.setAddress(rs.getString("address"));
                 cinema.setId(rs.getInt("cinema_id"));
                 cinema.setName(rs.getString("name"));
                 cinema.setVideoHallNum(rs.getInt("hall_num"));
                 cinemaList.add(cinema);
            }
            return cinemaList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public Cinema getCinemaById(int CinemaId) {
        Connection con = DBCon.getConnection();
        if(con == null){
            System.out.println("数据库连接失败");
        }
        try {
            PreparedStatement pStmt = con.prepareStatement("select * from table_cinema where cinema_id = ? ");
            pStmt.setInt(1,CinemaId);
            ResultSet rs = pStmt.executeQuery();
            Cinema cinema = new Cinema();
            if (rs.next()){
                cinema.setAddress(rs.getString("address"));
                cinema.setId(rs.getInt("cinema_id"));
                cinema.setName(rs.getString("name"));
                cinema.setVideoHallNum(rs.getInt("hall_num"));
            }
           return cinema;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Cinema> getCinemasByKey(String key) {
        Connection con = DBCon.getConnection();
        if(con == null){
            System.out.println("数据库连接失败");
        }
        try {
            PreparedStatement pStmt = con.prepareStatement("select * from table_cinema where name like ? ");
            pStmt.setString(1,key);
            ResultSet rs = pStmt.executeQuery();
            List<Cinema> cinemaList = new ArrayList<>();

            while (rs.next()){
                Cinema cinema = new Cinema();
                cinema.setAddress(rs.getString("address"));
                cinema.setId(rs.getInt("cinema_id"));
                cinema.setName(rs.getString("name"));
                cinema.setVideoHallNum(rs.getInt("hall_num"));
                cinemaList.add(cinema);
            }
            return cinemaList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean addCinema(Cinema cinema) {
        Connection conn = DBCon.getConnection();
        if(conn == null){
            System.out.println("数据库连接失败");
        }
        try {
            PreparedStatement pStmt = conn.prepareStatement("insert into table_cinema(name,hall_num,address) values(?,?,?)");
            pStmt.setString(1,cinema.getName());
            pStmt.setInt(2,cinema.getVideoHallNum());
            pStmt.setString(3,cinema.getAddress());
              int addNum =  pStmt.executeUpdate();
              if(addNum>0){
                  return true;
              }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

}
